package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.config.Config;
import com.xishu.entity.NameAndId;
import com.xishu.entity.foodpanda.Category;
import com.xishu.entity.foodpanda.Product;
import com.xishu.foodpanda.FoodPandaConfigService;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.ZhEnService;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(value = "category配置")
@RestController
public class CategoryController implements Tools {
    private static Logger logger = LoggerFactory.getLogger(CategoryController.class);
    private CommonService commonService = CommonService.getInstance();
    private Config config = Config.getInstance();

    @ApiOperation(value = "查询")
    @PutMapping(value = ("/category"), produces = ("application/json;charset=UTF-8"))
    public ResponseData query(@RequestBody Category category) throws Exception {
        return commonService.searchResponse(category);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = ("/category/modify"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData modify(@RequestBody Category category) throws Exception {
        VerifyUtil.verify(()->isNotEmpty(category.getId()),ResponseStatus.INVALID_PARAMETER);

        boolean modifyShowOrder = category.getShowOrder() != null;

        //修改菜里面的品类名
        Product product = new Product();
        product.setCategoryId(category.getId());
        product.setShopId(category.getShopId());
        List<Product> productList = commonService.searchAll(product);
        for (Product productInDb : productList) {
            //
            if (isNotEmpty(category.getName_zh()) || isNotEmpty(category.getName_en())) {

                if (isNotEmpty(category.getName_zh())) {
                    productInDb.setCategoryName_zh(category.getName_zh());
                }
                if (isNotEmpty(category.getName_en())) {
                    productInDb.setCategoryName_en(category.getName_en());
                }
                commonService.save(productInDb);
            }
        }

        ResponseData responseData = commonService.modifyObject(category);

        if (modifyShowOrder) {
            FoodPandaConfigService.getInstance().sortCategory(commonService.findEntity(category.getId(), Category.class));
        }

        return responseData;
    }

    @ApiOperation(value = "创建")
    @PostMapping(value = ("/category"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true, checkShopId = true)
    public ResponseData create(@RequestBody Category category) throws Exception {
        //处理国际化
        ZhEnService.getInstance().dealZhEn(category);
        return commonService.createObject(category, (c) -> {
            try {
                FoodPandaConfigService.getInstance().sortCategory(c);
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = ("/category"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData delete(@RequestBody Category category) throws Exception {
        boolean deleteCategory = FoodPandaConfigService.getInstance().canDeleteCategory(category.getId());

        if(!deleteCategory){
            Long id = category.getId();
            Product product = new Product();
            product.setCategoryId(id);
            List<Product> products = CommonService.getInstance().searchList(product);
            List<String> productNameZh = products.stream().map(p -> p.getName_zh()).collect(Collectors.toList());
            List<String> productNameEn = products.stream().map(p -> p.getName_en()).collect(Collectors.toList());
            VerifyUtil.verifyZhEn(() -> false, ResponseStatus.DELETE_CATEGORY,  new List[]{productNameZh,productNameZh},new List[]{productNameEn,productNameEn});
        }

        return commonService.delete(category);
    }

    @ApiOperation(value = "品类是否可以删除")
    @PutMapping(value = ("/category/can/delete"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData canDelete(@RequestBody Category category) throws Exception {
        return commonService.createResponse(FoodPandaConfigService.getInstance().canDeleteCategory(category.getId()));
    }


}

